﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using PrgsMgr.CStructure;

namespace PrgsMgr
{
    public partial class Progress : Form
    {
        /// <summary>
        /// 进度条开始的时间
        /// </summary>
        DateTime dt_Time;
        /// <summary>
        /// 进度条经过的时间
        /// </summary>
        TimeSpan dt_Pass;

        ThreadStart th_start;
        ParameterizedThreadStart th_start_p;
        object th_para = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public Progress(ThreadStart start)
        {
            InitializeComponent();
            this.LabTime.Text = "";
            this.labelStatus.Text = "";
            PubVal.ProcessHwnd = this.Handle;
            th_start = start;
        }
        public Progress(ParameterizedThreadStart start, object parameter)
        {
            InitializeComponent();
            this.LabTime.Text = "";
            this.labelStatus.Text = "";
            PubVal.ProcessHwnd = this.Handle;
            th_start_p = start;
            th_para = parameter;
        }
        /// <summary>
        /// 关闭窗体委托
        /// </summary>
        private delegate void dg_CloseFrm();
        /// <summary>
        /// 设置进度条委托
        /// </summary>
        /// <param name="_Enabled"></param>
        private delegate void dg_SetProcessEnabled(bool _Enabled);
        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void CloseFrm()
        {
            this.Close();
        }
        /// <summary>
        /// 设置进度条的显示
        /// </summary>
        /// <param name="_Enabled"></param>
        private void SetProcessEnabled(bool _Enabled)
        {
            if (_Enabled)
            {
                this.DoProgress.Start();
            }
            else
            {
                this.DoProgress.Stop();
            }
        }
        /// <summary>
        /// 消息拦截
        /// </summary>
        /// <param name="msg"></param>
        protected override void WndProc(ref Message msg)
        {
            PROGRESS_INFO progressInfo = new PROGRESS_INFO();
            switch ((WM_MSG)msg.Msg)
            {
                case WM_MSG.WM_MSG_PROCESS_UPDATE:
                    progressInfo = (PROGRESS_INFO)msg.GetLParam(progressInfo.GetType());
                    this.labelStatus.Text =progressInfo.Status;
                    if ((progressInfo.Value != null) && (progressInfo.Value != ""))
                    {
                        this.labelPercent.Text = progressInfo.Value;
                    }
                    break;
                case WM_MSG.WM_MSG_PROCESS_CLOSE_FRM:
                    this.BeginInvoke(new dg_CloseFrm(CloseFrm));
                    break;
                default:
                    break;
            }
            base.WndProc(ref msg);
        }
        /// <summary>
        /// 重绘窗体，画一条直线
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //Pen pen = new Pen(Color.Red);
            Point p1 = new Point(0,this.labelTitle.Location.Y + this.labelTitle.Height + 5);
            Size size = new Size(this.Width,2);
            Rectangle rect = new Rectangle(p1,size);
            e.Graphics.FillRectangle((Brush)Brushes.DarkGray, rect);
            
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Progress_Load(object sender, EventArgs e)
        {
            this.Invoke(new dg_SetProcessEnabled(SetProcessEnabled), true);
            dt_Time = DateTime.Now;
            if (th_para == null)
            {
                Thread th = new Thread(th_start);
                th.Start();
            }
            else
            {
                Thread th = new Thread(th_start_p);
                th.Start(th_para);
            }
        }
        /// <summary>
        /// 设置时间显示timer事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tTimeDisplay_Tick(object sender, EventArgs e)
        {
            dt_Pass = DateTime.Now - dt_Time;
            this.LabTime.Text = "时间：" + dt_Pass.ToString(@"hh\:mm\:ss");
        }
    }
}
